import React from 'react';
import { Tree } from 'antd';
const TreeNode = Tree.TreeNode;

class TreeNodes extends React.Component {
    state = {
        defaultExpandedKeys: ''
    }

    onSelect = (selectedKeys, info) => {
        if(!this.props.selectNode) {
            return;
        }
        if(selectedKeys.length === 0) {
            return;
        }
        this.props.selectNode(selectedKeys);
    }
    onCheck = (checkedKeys, info) => {
        this.props.checkNode(checkedKeys);
    }
    render() {
        const key = this.props.data.length > 0 ? [this.props.data[0].fId] : [];
        if(key.length > 0) {
            return (
                <Tree
                    checkable={this.props.checked}
                    onSelect={this.onSelect}
                    onCheck={this.onCheck}
                    defaultExpandedKeys={key}
                >
                    {
                        this.props.data.map( (item) => treeNodeCreat(item))
                    }
                </Tree>
            );
        }
        return null;
    }
}
/** 
 * 树节点递归生成方法
*/
function treeNodeCreat({fId, fName, fRoleName, children = []}) {
    return(
        <TreeNode title={fName||fRoleName} key={fId}>
        {
            children.length > 0 ? children.map( item => treeNodeCreat(item)) : null
        }
        </TreeNode>
    )
}

export default TreeNodes;